package com.jerry.constellation.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.jerry.constellation.R;

public class SplashActivity extends AppCompatActivity {

    private TextView splash_tv;
    int count = 5;
    Handler handler = new Handler() {
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1) {
                count--;
                if (count == 0) {
                    //判断是否第一次进入
                    boolean isFirst = sharedPreferences.getBoolean("isFirst", true);
                    Intent intent = new Intent();
                    if (isFirst) {
                        intent.setClass(SplashActivity.this, GuideActivity.class);
                        //为了下一次不跳转引导页，更改isFirst
                        SharedPreferences.Editor editor=sharedPreferences.edit();
                        editor.putBoolean("isFirst",false);
                        editor.commit();
                    } else {
                        intent.setClass(SplashActivity.this, MainActivity.class);
                    }
                    startActivity(intent);
                    finish();
                } else {
                    splash_tv.setText(String.valueOf(count));
                    handler.sendEmptyMessageDelayed(1, 500);
                }
            }
        }
    };
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        splash_tv = findViewById(R.id.splash_Tv);
        sharedPreferences = getSharedPreferences("firstOpen", MODE_PRIVATE);
        handler.sendEmptyMessageDelayed(1, 500);
    }
}